package nl.borchef.map_2d_game;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.GradientDrawable;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.util.Log;

public class game_map_2d { 
	private int[] mi_map_arr = {8,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,9,10,0,0,10,10,10,10,0,0,10,0,0,10,0,6,10,10,10,10,10,10,0,0,6,10,10,10,0,0,2,0,3,10,0,0,0,0,0,10,10,10,0,0,0,6,10,10,0,0,0,0,0,10,0,8,10,9,0,10,10,10,10,9,0,0,0,10,0,0,4,0,3,10,0,10,10,10,0,10,10,10,10,10,9,0,0,10,0,0,10,0,0,10,0,6,10,10,0,10,10,10,0,0,2,0,0,10,0,0,10,0,3,7,0,0,0,10,0,10,10,10,0,0,0,0,0,10,0,0,10,0,0,0,0,0,8,10,0,6,10,10,10,10,5,0,0,10,0,0,10,0,0,0,0,0,10,10,0,0,10,10,0,0,0,0,0,10,0,0,10,0,0,0,0,0,10,10,9,0,10,10,0,8,9,0,0,10,0,8,10,10,5,0,0,0,10,0,10,0,10,10,0,10,10,10,10,10,10,7,0,0,0,0,0,0,10,0,10,0,10,10,0,10,10,10,10,0,0,0,0,0,0,0,0,0,10,0,10,0,10,10,0,10,10,10,10,0,0,8,10,10,10,10,10,10,10,0,10,0,10,10,0,10,10,10,10,9,0,10,10,10,10,10,10,10,7,0,10,0,10,10,0,10,10,10,10,10,0,6,10,10,0,0,0,0,0,0,10,0,10,10,0,10,10,10,10,10,0,0,0,2,0,4,0,3,10,10,7,0,10,10,0,6,10,10,10,7,0,0,0,0,0,10,0,0,0,0,0,0,10,10,0,0,0,0,0,0,0,0,0,8,10,10,10,10,10,10,9,0,10,6,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,7};
//	private int[] mi_map_arr = {1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,0,1,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1};
//	private int[] mi_map_arr = {1,1,1,1,0,1,1,1,1};
	private Point mp_map_size = new Point(20,20);
	private Point mp_surface_pixel_size = new Point(0,0);
	private Point mp_number_off_surface_tiles_to_draw = new Point(0,0);
	private Point mp_map_tile_size = new Point(100,150);
	private Point mp_map_position_pixels = new Point(100,300);
	private Point mp_target_position_pixels = new Point(mp_map_position_pixels.x,mp_map_position_pixels.y);
	private Point mp_screen_middle_pixel_position = new Point(0,0);
	private Rect mr_r;

	private paint_colors mo_paint_colors;
	private GradientDrawable mo_drawable_tile1;
	
	private int mi_number_off_basic_tiles = 10;
	float[] mf_tile_type_free_arr = {16,16,16,16,16,16,16,16}; 
	private int mi_tile_type_free = 1;
	
	float[] mf_tile_type_top_arr = {0,0,0,0,16,16,16,16}; 
	private int mi_tile_type_top = 2;

	float[] mf_tile_type_right_arr = {16,16,0,0,0,0,16,16}; 
	private int mi_tile_type_right = 3;

	float[] mf_tile_type_bottom_arr = {16,16,16,16,0,0,0,0}; 
	private int mi_tile_type_bottom = 4;

	float[] mf_tile_type_left_arr = {0,0,16,16,16,16,0,0}; 
	private int mi_tile_type_left = 5;
	
	float[] mf_tile_type_top_right_arr = {0,0,0,0,0,0,16,16}; 
	private int mi_tile_type_top_right = 6;

	float[] mf_tile_type_top_left_arr = {0,0,0,0,16,16,0,0}; 
	private int mi_tile_type_top_left = 7;

	float[] mf_tile_type_bottom_right_arr = {16,16,0,0,0,0,0,0}; 
	private int mi_tile_type_bottom_right = 8;
	
	float[] mf_tile_type_bottom_left_arr = {0,0,16,16,0,0,0,0}; 
	private int mi_tile_type_bottom_left = 9;

	float[] mf_tile_type_all_arr = {0,0,0,0,0,0,0,0}; 
	private int mi_tile_type_all = 10;
	

	private GradientDrawable mo_drawable_tile2;
	public game_map_2d( paint_colors paint_colors ){
		// dome drawables
		mo_drawable_tile1 = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                new int[] { 0xFFFFFF00,0xFFFFFF55,0xFFFFFF99,0xFFFFFF55,
				0xFF777777 });
		mo_drawable_tile1.setShape(GradientDrawable.RECTANGLE);
		mo_drawable_tile1.setGradientRadius((float)(mp_map_tile_size.y));
		mo_drawable_tile1.setGradientType(GradientDrawable.RADIAL_GRADIENT);
		
		mo_drawable_tile2 = new GradientDrawable(GradientDrawable.Orientation.LEFT_RIGHT,
                new int[] { 0xFF777777, 0xFF777777,
				0xFF777777 });
		mo_drawable_tile2.setShape(GradientDrawable.RECTANGLE);
		mo_drawable_tile2.setGradientRadius((float)(mp_map_tile_size.y));
		mo_drawable_tile2.setGradientType(GradientDrawable.LINEAR_GRADIENT);
		mr_r = new Rect();
		
	}
	public void update_physics(tank tank ){
		int dir = (int)(  Math.atan2(mp_target_position_pixels.y - mp_map_position_pixels.y, mp_target_position_pixels.x - mp_map_position_pixels.x) * 180 / Math.PI);
        //Log.d(this.getClass().getName(), "dir=" + dir );

		tank.turn(dir);
		
		int speed = tank.get_speed();
		if( mp_map_position_pixels.x < mp_target_position_pixels.x  ){
			 
 			mp_map_position_pixels.x += speed; 
			if( mp_map_position_pixels.x > mp_target_position_pixels.x  ){
				mp_map_position_pixels.x = mp_target_position_pixels.x;
			}
		}
		if( mp_map_position_pixels.x > mp_target_position_pixels.x  ){
			mp_map_position_pixels.x -= speed; 
			if( mp_map_position_pixels.x < mp_target_position_pixels.x  ){
				mp_map_position_pixels.x = mp_target_position_pixels.x;
			}
		}
		if(  mp_map_position_pixels.y < mp_target_position_pixels.y  ){
			mp_map_position_pixels.y += speed; 
			if( mp_map_position_pixels.y > mp_target_position_pixels.y  ){
				mp_map_position_pixels.y = mp_target_position_pixels.y;
			}
		}
		if( mp_map_position_pixels.y > mp_target_position_pixels.y  ){
			mp_map_position_pixels.y -= speed; 
			if( mp_map_position_pixels.y < mp_target_position_pixels.y  ){
				mp_map_position_pixels.y = mp_target_position_pixels.y;
			}
		}
		if( mp_map_position_pixels.y == mp_target_position_pixels.y &&
			mp_map_position_pixels.x == mp_target_position_pixels.x	){
			tank.stop();
		}
			
		
	}
	public void draw(Canvas canvas ){

		int offset_screen_pixels_x = 0;
		int offset_screen_pixels_y = 0;
		offset_screen_pixels_x = -(mp_map_tile_size.x - (mp_screen_middle_pixel_position.x % mp_map_tile_size.x));
		offset_screen_pixels_y = -(mp_map_tile_size.y - (mp_screen_middle_pixel_position.y % mp_map_tile_size.y));

		int map_offset_pixels_x = mp_map_position_pixels.x % mp_map_tile_size.x;
		int map_offset_pixels_y = mp_map_position_pixels.y % mp_map_tile_size.y;

		int screen_center_tile_x = ( mp_screen_middle_pixel_position.x / mp_map_tile_size.x ) + 1;  
		int screen_center_tile_y = ( mp_screen_middle_pixel_position.y / mp_map_tile_size.y ) + 1;  
		
		
		int map_offset_tile_x = mp_map_position_pixels.x / mp_map_tile_size.x;
		int map_offset_tile_y = mp_map_position_pixels.y / mp_map_tile_size.y;

		int min_x = screen_center_tile_x - map_offset_tile_x;
		int max_x = min_x + mp_map_size.x;
		int min_y = screen_center_tile_y - map_offset_tile_y;
		int max_y = min_y + mp_map_size.y;
		int map_tile = 0;
		
		for( int i = 0; i < mp_number_off_surface_tiles_to_draw.y; i++ ){
			
			for( int j = 0; j < mp_number_off_surface_tiles_to_draw.x; j++ ){

				if( i >= min_y &&
					i < max_y &&
					j < max_x &&
					j >= min_x ){
					mr_r.set( ( ( j  * mp_map_tile_size.x ) + offset_screen_pixels_x ) - map_offset_pixels_x, 
							  (( i  * mp_map_tile_size.y ) + offset_screen_pixels_y ) - map_offset_pixels_y,
							  (( j  * mp_map_tile_size.x ) + offset_screen_pixels_x ) + mp_map_tile_size.x - map_offset_pixels_x , 
							  (( i  * mp_map_tile_size.y ) + offset_screen_pixels_y ) + mp_map_tile_size.y - map_offset_pixels_y  );
						map_tile = ((i - min_y ) *  mp_map_size.y ) + ( j - min_x );
						//Log.d(this.getClass().getName(), "map_tile=" + map_tile );
						
						
						if( mi_map_arr[map_tile] == mi_tile_type_free  ){

							mo_drawable_tile1.setCornerRadii(mf_tile_type_free_arr);
						}
						if( mi_map_arr[map_tile] == mi_tile_type_top  ){

							mo_drawable_tile1.setCornerRadii(mf_tile_type_top_arr);
						}
						if( mi_map_arr[map_tile] == mi_tile_type_right  ){

							mo_drawable_tile1.setCornerRadii(mf_tile_type_right_arr);
						}
						if( mi_map_arr[map_tile] == mi_tile_type_bottom  ){

							mo_drawable_tile1.setCornerRadii(mf_tile_type_bottom_arr);
						}
						if( mi_map_arr[map_tile] == mi_tile_type_left  ){

							mo_drawable_tile1.setCornerRadii(mf_tile_type_left_arr);
						}
						if( mi_map_arr[map_tile] == mi_tile_type_top_right  ){

							mo_drawable_tile1.setCornerRadii(mf_tile_type_top_right_arr);
						}
						if( mi_map_arr[map_tile] == mi_tile_type_top_left  ){

							mo_drawable_tile1.setCornerRadii(mf_tile_type_top_left_arr);
						}
						if( mi_map_arr[map_tile] == mi_tile_type_bottom_right  ){

							mo_drawable_tile1.setCornerRadii(mf_tile_type_bottom_right_arr);
						}
						if( mi_map_arr[map_tile] == mi_tile_type_bottom_left  ){

							mo_drawable_tile1.setCornerRadii(mf_tile_type_bottom_left_arr);
						}
						if( mi_map_arr[map_tile] == mi_tile_type_all  ){

							mo_drawable_tile1.setCornerRadii(mf_tile_type_all_arr);
						}
						
						if( mi_map_arr[map_tile] > 0 && mi_map_arr[map_tile] <= mi_number_off_basic_tiles  ){
							mo_drawable_tile1.setBounds(mr_r);
							mo_drawable_tile1.draw(canvas);
						}
						else{
							mo_drawable_tile2.setBounds(mr_r);
							mo_drawable_tile2.draw(canvas);
						}
				}
				
			}
		}
/*
		Log.d(this.getClass().getName(), "mi_target_position_pixels.x=" + mp_target_position_pixels.x );
		Log.d(this.getClass().getName(), "mi_target_position_pixels.y=" + mp_target_position_pixels.y );
		Log.d(this.getClass().getName(), "mp_map_tile_size.x=" + mp_map_tile_size.x );
		Log.d(this.getClass().getName(), "mp_map_tile_size.y=" + mp_map_tile_size.y );
		Log.d(this.getClass().getName(), "mp_screen_middle_pixel_position.x=" + mp_screen_middle_pixel_position.x );
		Log.d(this.getClass().getName(), "mp_screen_middle_pixel_position.y=" + mp_screen_middle_pixel_position.y );
		Log.d(this.getClass().getName(), "mp_map_position_pixels.x=" + mp_map_position_pixels.x );
		Log.d(this.getClass().getName(), "mp_map_position_pixels.y=" + mp_map_position_pixels.y );
		Log.d(this.getClass().getName(), "offset_screen_pixels_x=" + offset_screen_pixels_x );
		Log.d(this.getClass().getName(), "offset_screen_pixels_y=" + offset_screen_pixels_y );
		Log.d(this.getClass().getName(), "map_offset_pixels_x=" + map_offset_pixels_x );
		Log.d(this.getClass().getName(), "map_offset_pixels_y=" + map_offset_pixels_y );
		Log.d(this.getClass().getName(), "map_offset_tile_x=" + map_offset_tile_x );
		Log.d(this.getClass().getName(), "map_offset_tile.y=" + map_offset_tile_y );
		Log.d(this.getClass().getName(), "mp_number_off_surface_tiles_to_draw.x=" + mp_number_off_surface_tiles_to_draw.x );
		Log.d(this.getClass().getName(), "mp_number_off_surface_tiles_to_draw.y=" + mp_number_off_surface_tiles_to_draw.y );

		Log.d(this.getClass().getName(), "screen_center_tile_x=" + screen_center_tile_x );
		Log.d(this.getClass().getName(), "screen_center_tile_y=" + screen_center_tile_y );
		Log.d(this.getClass().getName(), "min_x=" + min_x );
		Log.d(this.getClass().getName(), "max_x=" + max_x );
		Log.d(this.getClass().getName(), "min_y=" + min_y );
		Log.d(this.getClass().getName(), "max_y=" + max_y );
*/
	}
	public void set_target_destination( float x, float y )
	{
		Log.d(this.getClass().getName(), "touched" + x );
		mp_target_position_pixels.x =  (mp_map_position_pixels.x + ((int) x - ( mp_surface_pixel_size.x / 2 )));
		mp_target_position_pixels.y = (mp_map_position_pixels.y + ( (int) y - ( mp_surface_pixel_size.y / 2 )));
	}
	public void set_canvas_width(int width){
		mp_surface_pixel_size.x = width;
		mp_screen_middle_pixel_position.x = mp_surface_pixel_size.x / 2;
		mp_number_off_surface_tiles_to_draw.x = ( mp_surface_pixel_size.x / mp_map_tile_size.x ) + 3; 
	}
	public void set_canvas_height(int height){
		mp_surface_pixel_size.y = height;
		mp_screen_middle_pixel_position.y = mp_surface_pixel_size.y / 2;
		mp_number_off_surface_tiles_to_draw.y = ( mp_surface_pixel_size.y / mp_map_tile_size.y ) + 3; 
	}
}